package net.j4love.zookeeper.quickstart;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author He Peng
 * @create 2017-05-15 14:20
 * @update 2017-05-15 14:20
 * @see
 */
public class DeleteZNode {

    private static final Logger LOG = LoggerFactory.getLogger(DeleteZNode.class);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 1000, null);
        zk.create("/del_test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        Stat stat = zk.exists("/del_test", null);
        if ((stat != null) && zk.getData("/del_test", null, null) != null) {
            zk.delete("/del_test",-1);  // if the given version is -1, it matches any node's versions
            LOG.debug("删除 /del_test 节点.");
        }

        if (zk.exists("/del_test",null) == null) {
            LOG.debug("节点 /del_test 删除成功.");
        }

        zk.close();
    }
}
